<%@ taglib uri="/WEB-INF/mrald.tld" prefix="mrald"%>

<%@ page import="org.mitre.mrald.util.*" %>
<%@ page import="org.mitre.mrald.util.User"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.IOException"%>
<%@ page import="org.mitre.lattice.graph.GraphSelect"%>
<%@ page import="org.mitre.lattice.graph.GraphUserLattice"%>
<%@ page import="org.mitre.lattice.lattice.*"%>
<mrald:validate />
<%

        String action = request.getParameter( "action" );

	if (action.startsWith("Undo"))
	{

        MraldOutFile.logToFile(Config.getProperty("LOGFILE"), "LatticeServerUtils:   Undo");

        User mraldUser = (User)session.getAttribute(Config.getProperty("cookietag"));

        String latticeDir = Config.getProperty("latticeDir");
        MraldOutFile.logToFile(Config.getProperty("LOGFILE"), "LatticeServerUtils:   LatticeDir :" + latticeDir);

        String oldfileName = latticeDir + "backup/last/lattice.srv";
        MraldOutFile.logToFile(Config.getProperty("LOGFILE"), "LatticeServerUtils:   Backup LatticeDir :" + oldfileName);

        String newfileName = latticeDir + "lattice.srv";
        MraldOutFile.logToFile(Config.getProperty("LOGFILE"), "LatticeServerUtils:   New file Name:" + newfileName);

        FileUtils fileUtils = new FileUtils();
        File new_file = new File( newfileName);

        if (!new File(oldfileName).exists())
        {
            boolean created = new File(oldfileName).createNewFile();
            if (!created)
            {
                throw new IOException("Couldn't create the directory \"" + Config.getProperty("LatticeFile") +
                    "\".  Check your file system, though, some intermediate directories may have been created");
            }
        }
        File backupfile = new File( oldfileName);

        File prevGraph = new File(Config.getProperty("BasePath") + "/" + GraphSelect.getPrevGraph( mraldUser) + ".jpg");
        File lastGraph = new File(Config.getProperty("BasePath") + "/" + GraphSelect.getLastGraph( mraldUser) + ".jpg");

        fileUtils.copyFile(backupfile, new_file);
        fileUtils.copyFile(prevGraph, lastGraph);

        //LatticeFunctions.setIsSet(false);
        //LatticeTree lattice = LatticeFunctions.recoverLatticeTree(newfileName);
        //LatticeFunctions.setRootNode( lattice.getRootNode());

        GraphUserLattice.resetLattice();

        response.sendRedirect( "FormSubmit?workflow=Redraw%20Graph" );
	}

%>
